<DIV id="mainContainer">
<?php
	if(isset($_POST['path']))
	{
		require_once('Change_photo.php');
		change_path($_POST['path'],$_SESSION['info']['id_user']);
		require_once("session.php");
		$S=new Session();
		$_SESSION["info"] = $S->Get_info($_SESSION["id"]);

	}
	
	if(!isset($_GET['S']))
	{
		$info=$_SESSION["info"];
		$bool=1;
	}else{
		require("session.php");
		$session=new Session();
		$info=$session->Get_info_id($_GET['S']);
		$bool=0;
	}

	//if($_GET['S']==$info["id_user"])
	//{
	//$firstname= $info->firstname //$S->Get_firstname($_SESSION["id"]);
	//$lastname=$S->Get_lastname($_SESSION["id"]);
	if(strlen($info['firstname'])==0)
	{	
		$info['firstname']="Unknown";	
	}
	if(is_null($info['lastname']))
	{	
		$info['lastname']="Unknown";	
	}
	if(is_null($info['pseudo']))
	{	
		$info['pseudo']="Unknown";	
	}	
	if(is_null($info['photopath']))
	{	
		$info['photopath']="img/basic";	
	}	
	///////////////////////////////////////////////////////////
	    // PHOTO CHOICE ////
	///////////////////////////////////////////////////////////
	echo "<DIV id='leftCol'>";
		echo "<img src=\"".$info['photopath'].".jpg\" style=\"WIDTH: 179px\">";
		if($bool==1){
			echo "<br/> Photo:
			<DIV class=list>
		
			<FORM action=\"main.php?page=profil\" method=\"post\" style=\"margin: auto\" value=\"han\">
			<SELECT name=\"path\" size=\"1\" >
			<OPTION selected=\"selected\">
			<OPTION>chewie
			<OPTION>basic
			<OPTION>frozenhan
			<OPTION>han
			<OPTION>punk
			<OPTION>storm
			<OPTION>superman
			<OPTION>yoda
			</SELECT>
			<input class=\"fbbutton\" type=\"submit\"  value=\"submit\"/>
			</FORM>
			</DIV>";
		}

	///////////////////////////////////////////////////////////
	///////////// EDIT PSD AND PROFIL /////////////////////////
	///////////////////////////////////////////////////////////
	
	echo "</DIV>";
	
	echo "<DIV id='contentCol'>";
	echo "<DIV class='rfloat'>
		<form action=\"main.php?page=editprofile\" method=\"post\" style=\"margin: auto\">
			<input class='fbbutton' type='submit' name='editProfile' value='Edit Profile' /> 
			<input class='fbbutton' type='submit' name='editpsd' value='Change password' /> 
		</form>
		</DIV>";
	echo "<TABLE>";
	echo	"<h1> ".$info['pseudo']." </h1>";
	echo	"<TR><TH> Firstname </TH><TH>".$info['firstname']."</TH></TR>";
	echo	"<TR><TH> Name </TH><TH>".$info['lastname']."</TH></TR>";
	echo	"<TR><TH> Email </TH><TH><a href=\"mailto:".$info["mail"]."\">".$info["mail"]."</a></TH></TR>";
	echo	"</TABLE>";
	
	
    /////////////////////////////////////////////////////////////
					//Publish button//
	/////////////////////////////////////////////////////////////
	
	if(isset($_POST['postmessage'])&& $_POST["text"]!="") {
			  
		$request = "INSERT INTO msg (id_from, id_to,`date`,id_father,content)
					Values ('".$_SESSION["info"]["id_user"]."','".$info["id_user"]."',now(),'0',\"".mysql_real_escape_string($_POST["text"])."\")";
			  
		$dbin->query($request);
			  
		//echo "<meta http-equiv='refresh' content='0;URL=main.php?page=profil&S=".$info['id_user']."\">";
		
	}
	
	echo "<DIV class=\"fbcontentdivider\">
			</DIV>
			<DIV class=\"storyContainer\">
				<form action=\"main.php?page=profil&S=".$info['id_user']."\"  \" method=\"post\"> 
					<textarea name=\"text\" style='min-width: 300px; min-height: 30px; '></textarea><br/>
					<input class=\"fbbutton\" type=\"submit\" value=\"Publish\" name=\"postmessage\"/>  
				</form> 
			</DIV>
			<DIV class=\"fbcontentdivider\">
			</DIV>";
	
	/////////////////////////////////////////////////////////////
					//WALL//
	/////////////////////////////////////////////////////////////
	

	$request = "select distinct id_msg, u1.pseudo as psto,u2.pseudo as psfrom, id_from, id_to, date_format(`date`,'%d/%m/%y %H:%i') as d, id_father, content 
	from msg as m, user as u1, user as u2, friendship as f 
	where f.id1 = \"".$info["id_user"]."\" and m.id_to=f.id1 and id_father=0 and u1.id_user=m.id_to and u2.id_user=m.id_from 
	order by date desc limit 0,20";
	
	$dbin = new DB();
	$dbin->query($request);

	$dbin->getrownumber();
	if($dbin->nbrow==0){
			if($bool==1){
			echo "No one has written on your wall ! You should consider having more friends... Freak !";
			}else{
			echo "No one has wrote on my wall! Be the first to do so !";
			}
	}else{

		$j=1;
		while($dbin->fetch_assoc()){
			
				echo "<DIV class=\"storyContainer\">";
				if ($dbin->row['id_from']==$dbin->row['id_to']){
					echo "<a href=main.php?page=profil&S=".$dbin->row['id_from'].">".$dbin->row['psfrom']."</a>";
				}else{
					echo "<a href=main.php?page=profil&S=".$dbin->row['id_from'].">".$dbin->row['psfrom']."</a> => <a href=main.php?page=profil&S=".$dbin->row['id_to'].">".$dbin->row['psto']."</a>";
				}
				echo " <DIV class='firstMsg'> ".$dbin->row['content']." <br/><DIV style='color: #999999;'>".$dbin->row['d']."</DIV></DIV>";
			
				echo "<DIV class='msgBox'>";
				$request2 = "select distinct pseudo, id_from, date_format(`date`,'%d/%m/%y %H:%i') as d, content from msg as m, user as u, friendship as f 
							where id_father = ".$dbin->row['id_msg']." and u.id_user=m.id_from 
							order by `date`";
						
				$dbin2 = new DB();
				$dbin2->query($request2);
			
				// it's a request in a request to get commentaries on commentaries
				while($dbin2->fetch_assoc()){
					echo "<DIV class='comment'><a href=main.php?page=profil&S=".$dbin2->row['id_from'].">".$dbin2->row['pseudo']." </a>".$dbin2->row['content']." <br/><DIV style='color: #999999;'> ".$dbin2->row['d']."</DIV></DIV>";
				}
				echo "<DIV class='comment'><form action=# method=\"post\" id=\"myForm\">" ;
				echo "<input type =\"hidden\" name = \"com".$j."\" value =\"".$dbin->row['id_msg']."\" /> ";
				echo "<input type =\"hidden\" name = \"pers".$j."\" value =\"".$dbin->row['id_to']."\" /> "; 	
				echo "<input type=\"text\" name=\"text".$j."\" value=\"\" size=\"45\" />";
				echo "<input class='fbbutton rfloat'type=\"submit\" name=\"val".$j."\" value=\"Comment\" /> ";
				echo "</form></DIV>	";
				echo "</DIV>";

				
				$j=$j+1;
				echo "</DIV><DIV class='fbcontentdivider'></DIV>";
		}
		
		 for($i=1;$i < 21;$i++) 
		 { 
			  if(isset($_POST["val".$i])&& $_POST["text".$i]!=""){
			  
			  $request = "INSERT INTO msg (id_from, id_to,`date`,id_father,content)
						Values ('".$_SESSION["info"]["id_user"]."','".$_POST["pers".$i]."',now(),'".$_POST["com".$i]."',\"".$_POST["text".$i]."\")";
			  
			  $dbin->query($request);
			  
			  echo "<meta http-equiv='refresh' content='0;URL=main.php?page=profil&S=".$info['id_user']."\">";
			  
			  }
		 } 
	}	 
	echo "</DIV>";

?>

</DIV>


